import heapq, random
import chess

class Player():
  def __init__(self, name='Player'):
    self.name = name
  
  def __str__(self):
    return self.name
  
  def get_move(self, board):
    assert False, 'Not implemented for %s' % self
  
class HumanPlayer(Player):
  def get_move(self, game):
    return raw_input(game.get_prompt())
  
class RandomPlayer(Player):
  def get_move(self, game):
    return str(random.choice(game.state['legal_moves']))

class KillRandomPlayer(Player):
  def get_move(self, game):
    pq = PriorityQueue()
    for move in game.state['legal_moves']:
      if move.capture:
        pq.push(move, 0)
      else:
        pq.push(move, 1)
    kill_moves = []
    weak_moves = []
    while not pq.is_empty():
      move = pq.pop()
      if move.capture:
        kill_moves.append(move)
      else:
        weak_moves.append(move)
    if len(kill_moves) > 0:
      return str(random.choice(kill_moves))
    else:
      return str(random.choice(weak_moves))

class KillFirstPawnSecond(Player):
  def get_move(self, game):
    kill_moves = []
    pawn_moves = []
    weak_moves = []
    for move in game.state['legal_moves']:
      moving_piece = game.get_piece(move.start_position)
      if move.capture:
        kill_moves.append(move)
      elif moving_piece == chess.WHITE_PAWN or moving_piece == chess.BLACK_PAWN:
        pawn_moves.append(move)
      else:
        weak_moves.append(move)
    if len(kill_moves) > 0:
      return str(random.choice(kill_moves))
    elif len(pawn_moves) > 0:
      return str(random.choice(pawn_moves))
    else:
      return str(random.choice(weak_moves))
    

class PriorityQueue():
  def __init__(self):
    self.heap = []
  
  def push(self, item, value):
    heapq.heappush(self.heap, (value, item))
  
  def pop(self):
    blob = heapq.heappop(self.heap)
    return blob[1]
  
  def is_empty(self):
    return len(self.heap) == 0
